package com.clementheliou.cheliou.web.util.jsf;

import static javax.faces.context.FacesContext.getCurrentInstance;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.clementheliou.cheliou.business.technical.i18n.I18nTechnicalService;
import com.clementheliou.cheliou.web.controller.user.UserBean;

/**
 * Utilities about <tt>JSF2</tt> framework.
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @see {@link Component}.
 * @since 1.0
 */
@Component
public class JSFUtils {

	/**
	 * Translation service.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Autowired};
	 * @see {@link I18nTechnicalService}.
	 * @since 1.0
	 */
	@Autowired
	private I18nTechnicalService i18nService;

	/**
	 * Utilities about the user details.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Autowired};
	 * @see {@link UserBean}.
	 * @since 1.0
	 */
	@Autowired
	private UserBean userBean;

	/**
	 * Adds a {@link FacesMessage} with the {@link FacesMessage#SEVERITY_INFO}
	 * severity. No details will be displayed.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @param messageKey the key of the message to be displayed.
	 * @since 1.0
	 */
	public void addInfoMessage(final String messageKey) {
		getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, i18nService.getTranslation(messageKey, userBean.getCurrentLocale()), null));
	}

	/**
	 * Adds a {@link FacesMessage} with the {@link FacesMessage#SEVERITY_ERROR}
	 * severity. No details will be displayed.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @param messageKey the key of the message to be displayed.
	 * @since 1.0
	 */
	public void addErrorMessage(final String messageKey) {
		getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, i18nService.getTranslation(messageKey, userBean.getCurrentLocale()), null));
	}

	/**
	 * Gets the request included inside the current {@link ExternalContext}.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @return the request included inside the current {@link ExternalContext}.
	 * @see {@link HttpServletRequest}.
	 * @since 1.0
	 */
	public HttpServletRequest getExternalContextRequest() {
		return (HttpServletRequest) getCurrentInstance().getExternalContext().getRequest();
	}

	/**
	 * Gets the response included inside the current {@link ExternalContext}.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @return the response includes inside the current {@link ExternalContext}.
	 * @see {@link HttpServletResponse}.
	 * @since 1.0
	 */
	public HttpServletResponse getExternalContextResponse() {
		return (HttpServletResponse) getCurrentInstance().getExternalContext().getResponse();
	}

	/**
	 * Gets the current {@link HttpSession}; i.e. the one included inside the
	 * current {@link ExternalContext}. If the session does not exist, it won't
	 * be created.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @return the current {@link HttpSession}.
	 * @see {@link ExternalContext};
	 * @see {@link HttpSession}.
	 * @since 1.0
	 */
	public HttpSession getCurrentSession() {
		return (HttpSession) getCurrentInstance().getExternalContext().getSession(false);
	}
}
